Skip to content

fix(plugin-react-swc): support bundledDev mode (fix #1271)#1279

Open
AmariahAK wants to merge 1 commit into
vitejs:mainfrom
AmariahAK:main
Open

fix(plugin-react-swc): support bundledDev mode (fix #1271)#1279
AmariahAK wants to merge 1 commit into
vitejs:mainfrom
AmariahAK:main

Conversation

@AmariahAK

Copy link
Copy Markdown

Description

fixes #1271

### What was the issue

When `experimental.bundledDev` is enabled in Vite config, `@vitejs/plugin-react-swc`'s `transformIndexHtml` hook crashes with:

TypeError: Cannot read properties of undefined (reading 'config')
at PluginContextImpl.transformIndexHtml (…/@vitejs/plugin-react-swc/index.js:137:46)


This happens because in bundledDev mode Vite uses a build pipeline rather than the dev server, so `config.server` is `undefined` — but the plugin unconditionally accesses `config.server!.config.base` to build the React refresh preamble URL.

### Where was the issue

`packages/plugin-react-swc/src/index.ts`, line 173 — the `transformIndexHtml` handler:

```ts
children: getPreambleCode(config.server!.config.base),

How it was fixed

Detected bundledDev mode in configResolved via config.experimental.bundledDev, and branched the transformIndexHtml handler to use getPreambleCode('/') (hardcoded / base) when in bundledDev mode. In bundled dev, assets are served as static files and don't go through the dev server middlewares, so the configured base prefix isn't needed.

This follows the exact same pattern already implemented in @vitejs/plugin-react (commit 6c57dd4).

Why this method

The alternative of trying to extract the base from another config source would be fragile and inconsistent. The reference implementation in plugin-react already solved this correctly by using '/' in bundledDev — bundling produces static assets at predictable paths that don't need URL rewriting.

How to test

# Run the new test
pnpm --filter @vitejs/plugin-react-swc exec playwright test --grep "bundled-dev"

# Or reproduce manually: create a Vite project with @vitejs/plugin-react-swc,
# enable experimental.bundledDev: true, and run `vite` — no longer crashes.

Changes

  • packages/plugin-react-swc/src/index.ts — detect bundledDev in configResolved, branch transformIndexHtml to use getPreambleCode('/') when active (+16/-2 lines)
  • packages/plugin-react-swc/playground/bundled-dev/ — new test playground verifying the server starts without error (7 files)
  • pnpm-lock.yaml — updated for the new playground

All 22 existing tests continue to pass; the new bundled-dev test passes.

In bundledDev mode, config.server is undefined, causing
transformIndexHtml to crash when accessing config.server.config.base.
Detect bundledDev in configResolved and use getPreambleCode('/')
instead, matching the fix already applied in plugin-react (6c57dd4).

Add bundled-dev test playground to verify the server starts without
the config.server crash.

Co-authored-by: atlarix-agent <agent@atlarix.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants